<?php
//$moduleid = 30;
include '../../common.inc.php';
include __DIR__.'/auto.func.php';
$dbMain   = new PDO('mysql:host=192.168.1.19;dbname=ixdcw', 'root', 'root');
$dbSource = new PDO('mysql:host=192.168.1.19;dbname=caiji', 'root', 'root');
$table    = 'sohu_baoyang';
$aimTable = 'xd_maintaince';
$aimData  = 'xd_maintaince_data';
$ds       = $dbSource->query('select max(id) as num from ' . $table);
$fo       = $ds->fetchObject();
$maxId      = intval($fo->num);
if (!isset($id)) {
	$id = 0;
} else {
	if ($id >= $maxId) {
		die(date('Y-m-d H:i:s'));
	}
}
$sql     = "select * from {$table} where id > $id and yifa=0 limit 80";
$result  = $dbSource->query($sql);
$rs      = $result->fetchAll();
/**
'Id' => string '102159' (length=6)
'pinpai' => string '大众途安' (length=12)
'niankuan' => string '2008款' (length=7)
'rongsheng' => string '2.0L' (length=4)
'name' => string ' 2.0L 手动 智享版5座' (length=26)
'info' => string '' (length=0)
'yifa' => string '0' (length=1)
 */
foreach ($rs as $r) {
	$id = $r['Id'];
	var_dump($r['Id']);
	if ($r['info']) {
		$catid =  3591 ; // catid
		$ranks = autoGetMatchedIds($r['pinpai']);
		$content    = $dbMain->quote($r['info']);
		$title      = $r['pinpai'].' '.$r['niankuan'].' '.$r['rongsheng'].' '.$r['name'];
		$addtime = time() - rand(0, 86400*120);
		//thumb

		$edittime = time();
		$hits = rand(30, 5000);
		$insertMain = "insert into {$aimTable} (catid,arbid, armid, arcid,title, addtime, edittime, hits, status) values(
			$catid,
			$ranks[0],
			$ranks[1],
			$ranks[2],
			'$title',
			'$addtime',
			'$edittime',
			'$hits',
			3
		)";

		$dbMain->query($insertMain);
		$insertId = $dbMain->lastInsertId();
		$insertData = "insert into $aimData values ($insertId, $content)";
		if (!$dbMain->query($insertData)) {
			var_dump($insertMain);
			exit($insertData);
		}
	}

	$dbSource->query(Sql::update($table, 'yifa=1', 'id='.$r['Id']));
//	exit($start);
}

echo '<meta http-equiv="refresh" content="1; url=' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&debug=xddl" />';